package com.pureut.storage.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.storage.dto.NewPickingSaleDetailDto;
import com.pureut.storage.mapper.PickingSaleDetailMapper;
import com.pureut.storage.service.PickingSaleDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 销售发货明细
 * @author: CM
 * @date: 2023/7/24  15:15
 */

@Service
@AllArgsConstructor
public class PickingSaleDetailServiceImpl implements PickingSaleDetailService {

    private final PickingSaleDetailMapper pickingSaleDetailMapper;

    /**
     * 单位远程调用
     */
    private final RemoteUnitService remoteUnitService;

    /**
     * 销售发货明细列表
     * @param wodDocNum
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NewPickingSaleDetailDto> getList(String wodDocNum, PageQuery pageQuery) {
        Page<NewPickingSaleDetailDto> list = pickingSaleDetailMapper.getPickDetailList(pageQuery.build(),wodDocNum);

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        //明细状态字典
        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "picking_sale_detail_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (NewPickingSaleDetailDto newPickingSaleDetailDto : list.getRecords()) {
            newPickingSaleDetailDto.setWodStatus(newPickingSaleDetailDto.getWodStatus());
            if(unitByCodeToMap.get(String.valueOf(newPickingSaleDetailDto.getMaterialUnit())) != null){
                //添加单位Dict
                newPickingSaleDetailDto.setMaterialUnitDict(unitByCodeToMap.get(String.valueOf(newPickingSaleDetailDto.getMaterialUnit())).getUnitName());
            }
            //String unitName = remoteUnitService.getUnitDataByCode(newPickingSaleDetailDto.getMaterialUnit()).getData().getUnitName();

            //添加单位
            newPickingSaleDetailDto.setMaterialUnit(newPickingSaleDetailDto.getMaterialUnit());

            //添加明细状态和明细
            newPickingSaleDetailDto.setWodStatus(newPickingSaleDetailDto.getWodStatus());
            newPickingSaleDetailDto.setWodStatusDict(statusMap.get(newPickingSaleDetailDto.getWodStatus()));

        }
        return TableDataInfo.build(list);
    }

}
